package com.cy.pj.sys.service;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.sys.dao.SysUserDao;
import com.cy.pj.sys.dao.SysUserRoleDao;
import com.cy.pj.sys.pojo.SysUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Transactional(rollbackFor = Throwable.class,isolation = Isolation.READ_COMMITTED)
@Service
public class SysUserServiceImpl implements SysUserService{
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private SysUserRoleDao sysUserRoleDao;


    @Override
    public int updatePassword(String sourcePassword, String newPassword, String confirmPassword) {
        if (sourcePassword==null || "".equals(sourcePassword))
            throw new IllegalArgumentException("原密码不能为空");
        if (newPassword==null || "".equals(newPassword))
            throw new IllegalArgumentException("新密码不能为空");
        if (!newPassword.equals(confirmPassword))
            throw new IllegalArgumentException("两次输入的密码不一致");
        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        SimpleHash sh = new SimpleHash("MD5",sourcePassword,user.getSalt(),1);
        if (!user.getPassword().equals(sh.toHex()))
            throw new IllegalArgumentException("密码不正确");
        String newSalt= UUID.randomUUID().toString();
        sh=new SimpleHash("MD5",newPassword,newSalt,1);
        String newHashPassword=sh.toHex();
        int rows = sysUserDao.updatePassword(newHashPassword, newSalt, user.getId());
        return rows;
    }

    //@RequiresPermissions("sys:user:view")
    @Override
    public List<SysUser> findUsers(SysUser entity) {
        return sysUserDao.selectUsers(entity);
    }


    @Transactional(readOnly = true)
    @Override
    public SysUser findById(Integer id) {
        SysUser user = sysUserDao.selectById(id);
        if (user==null)
            throw new ServiceException("没有找到对应的用户");
        List<Integer> roleIds=sysUserRoleDao.selectRoleIdsByUserId(id);
        user.setRoleIds(roleIds);
        return user;
    }

    @Override
    public int saveUser(SysUser entity) {
        //对密码进行加密(MD5是一种不可逆的加密算法，只能加密不能解密)
        String password = entity.getPassword();
        String salt = UUID.randomUUID().toString();
        SimpleHash simpleHash = new SimpleHash("MD5",password,salt,1);
        password=simpleHash.toHex();
        entity.setSalt(salt);
        entity.setPassword(password);
        int rows = sysUserDao.insertUser(entity);
        sysUserRoleDao.insertUserRoles(entity.getId(), entity.getRoleIds());
        return rows;
    }


    @Override
    public int updateUser(SysUser entity) {
        int rows=sysUserDao.updateUser(entity);
        if (rows==0)
            throw new ServiceException("该记录可能已经不存在");
        sysUserRoleDao.deleteByUserId(entity.getId());
        sysUserRoleDao.insertUserRoles(entity.getId(),entity.getRoleIds());
        return rows;
    }

    //@RequiresPermissions("sys:user:update")
    @Transactional
    @Override
    public int validById(Integer id, Integer valid) {
        //String modifiedUser = "admin";
        //获取登录用户
        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        String modifiedUser = user.getUsername();
        int rows=sysUserDao.validById(id,valid,modifiedUser);
        if (rows==0)
            throw new ServiceException("该记录可能已经不存在了");
        return rows;
    }
}
